package org.lanqiao.myutil;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import sun.misc.BASE64Encoder;

public class Myutils {
	// 适应MD5加密
	public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		// 确定计算方法
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		BASE64Encoder base64en = new BASE64Encoder();
		// 加密后的字符串
		String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
		return newstr;
	}

	// 把字符串转为数字
	public static float changeToFloat(String str) {
		return Float.valueOf(str);
	}

	// 把日期字符串格式规范化
	public static String normTime(String str) {
		String[] strNew = str.split("/");
		return strNew[2] + "/" + strNew[0] + "/" + strNew[1];
	}

	// 把时间向前推一个月
	public static Date getBeforeTime() throws ParseException {
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = format.format(date);
		String[] da = time.split("-");
		int year = Integer.valueOf(da[0]);
		int month = Integer.valueOf(da[1]);
		if (month == 1) {
			month = 12;
			year -= 1;
		} else {
			month -= 1;
		}
		String before = year + "-" + month + "-" + da[2];
		Date be = format.parse(before);
		return be;
	}
	
	//获取时间的字符串
	public static String getTimeToString(Date date){
		DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		String str=format.format(date);
		return str;
	}

}
